package com.pig.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import sun.audio.AudioData;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
import sun.audio.ContinuousAudioDataStream;

/**
 * 连续播放音乐
 * 
 * @author Domili
 * 
 */
public class ContinuousMusic {

	private ContinuousAudioDataStream cads;

	public AudioData ad;

	public ContinuousMusic(String musicPath) {
		load(musicPath);
	}

	/**
	 * 按路径加载文件
	 * 
	 * @param musicPath
	 *            文件相对路径
	 */
	public void load(String musicPath) {

		FileUtil fUtil = new FileUtil();
		String musicRealPath = fUtil.getFilePath(musicPath);
		try {
			InputStream inStream = new FileInputStream(musicRealPath);
			AudioStream as = new AudioStream(inStream);
			ad = as.getData();
			cads = new ContinuousAudioDataStream(ad);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 播放连续音乐
	 */
	public void play() {
		AudioPlayer.player.start(cads);
	}

	/**
	 * 停止播放
	 */
	public void stop() {
		AudioPlayer.player.stop(cads);
	}
}
